﻿package mortal.game.view.flowerSend
{
    import Message.Game.*;
    import Message.Public.*;
    import com.mui.utils.*;
    import extend.language.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.utils.*;
    import mortal.game.view.flowerSend.data.*;
    import mortal.game.view.flowerSend.effect.*;
    import mortal.game.view.flowerSend.panel.*;
    import mortal.mvc.core.*;

    public class FlowerSendController extends Controller
    {
        private var _nomalFlowerWin:FlowerReceivedBaseWin;
        private var _currentflowerWin:FlowerSendBaseWin;
        private var _flowerSenderList:Array;

        public function FlowerSendController()
        {
            this._flowerSenderList = [];
            return;
        }// end function

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.FlowerSend, this.flowerSend);
            Dispatcher.addEventListener(EventName.FlowerOpenWin, this.flowerOpenWin);
            Dispatcher.addEventListener(EventName.FlowerCloseWin, this.flowerCloseWin);
            Dispatcher.addEventListener(EventName.FlowerKissBack, this.kissBack);
            NetDispatcher.addCmdListener(ServerCommand.FlowerReceived, this.flowerReceived);
            NetDispatcher.addCmdListener(ServerCommand.FlowerSendSuccess, this.flowerSendSuccess);
            NetDispatcher.addCmdListener(ServerCommand.FlowerBroadCast, this.flowerBroadCast);
            return;
        }// end function

        private function kissBack(event:DataEvent) : void
        {
            GameProxy.friend.kissBack(event.data as String);
            return;
        }// end function

        private function flowerCloseWin(event:DataEvent) : void
        {
            if (this._currentflowerWin)
            {
                this._currentflowerWin = null;
            }
            return;
        }// end function

        private function flowerSend(event:DataEvent) : void
        {
            var _loc_2:* = event.data as FlowerSendData;
            if (Cache.instance.role.entityInfo.level < GameDefConfig.instance.getSystemOpenLevel(103))
            {
                return MsgManager.showRollTipsMsg(Language.getStringByParam(35298, GameDefConfig.instance.getSystemOpenLevel(103)), MsgRollTipsType.msgRollTips3);
            }
            if (_loc_2.itemData.itemInfo.effect == 999 && Cache.instance.vip.level < 1)
            {
                return MsgManager.showRollTipsMsg(Language.getString(35299), MsgRollTipsType.msgRollTips3);
            }
            GameProxy.friend.sendFlowers(_loc_2);
            return;
        }// end function

        private function sortFlower(param1:ItemData, param2:ItemData) : int
        {
            if (param1.itemInfo.type < param2.itemInfo.type)
            {
                return -1;
            }
            if (param1.itemInfo.type == param2.itemInfo.type)
            {
                if (param1.itemInfo.effect < param2.itemInfo.effect)
                {
                    return -1;
                }
                if (param1.itemInfo.effect == param2.itemInfo.effect)
                {
                    if (ItemsUtil.isBind(param1))
                    {
                        return -1;
                    }
                    return 1;
                }
                else
                {
                    return 1;
                }
            }
            else
            {
                return 1;
            }
        }// end function

        private function flowerOpenWin(event:DataEvent) : void
        {
            var _loc_4:Array = null;
            var _loc_2:* = event.data as FlowerSendData;
            if (_loc_2.itemData == null)
            {
                _loc_4 = Cache.instance.pack.backPackCache.getItemsByGroupAndCategory(EGroup._EGroupProp, EProp._EPropFlower);
                if (_loc_4 && _loc_4.length)
                {
                    _loc_4.sort(this.sortFlower);
                    _loc_2.itemData = _loc_4[0];
                }
                else
                {
                    _loc_2.itemData = new ItemData(300001000);
                }
            }
            var _loc_3:* = _loc_2.itemData;
            if (ItemsUtil.isFlower(_loc_3))
            {
                if (this._currentflowerWin == null)
                {
                    this._currentflowerWin = UICompomentPool.getUICompoment(FlowerSendBaseWin);
                    this._currentflowerWin.show();
                }
                this._currentflowerWin.data = _loc_2;
                this._currentflowerWin.currentState = FlowerSendBaseWin.sending;
            }
            return;
        }// end function

        private function flowerSendSuccess(param1:Object) : void
        {
            var _loc_2:* = param1 as FlowerSendData;
            if (this._currentflowerWin)
            {
                this._currentflowerWin.data = _loc_2;
                this._currentflowerWin.currentState = FlowerSendBaseWin.sendSuccess;
            }
            return;
        }// end function

        private function flowerReceived(param1:Object) : void
        {
            var _loc_2:* = param1 as SFlowerInfoMsg;
            this._flowerSenderList.push(_loc_2);
            this.showNextFlowerWin();
            return;
        }// end function

        private function flowerBroadCast(param1:SFlowerNoticeMsg) : void
        {
            var _loc_2:* = new ItemData(param1.itemCode);
            if (_loc_2.itemInfo.effect == 999)
            {
                FlowerEffectUtil.playEffect(param1);
            }
            return;
        }// end function

        public function showNextFlowerWin() : void
        {
            if (this._flowerSenderList.length == 0)
            {
                return;
            }
            if (this._nomalFlowerWin != null && !this._nomalFlowerWin.isHide)
            {
                return;
            }
            var _loc_1:* = this._flowerSenderList.shift();
            var _loc_2:* = new ItemData(_loc_1.itemCode);
            if (ItemsUtil.isFlower(_loc_2))
            {
                if (this._nomalFlowerWin == null)
                {
                    this._nomalFlowerWin = UICompomentPool.getUICompoment(FlowerReceivedBaseWin);
                }
                this._nomalFlowerWin.show();
                this._nomalFlowerWin.data = _loc_1;
            }
            return;
        }// end function

    }
}
